package org.forwhat.service.Impl;

import org.apache.ibatis.session.SqlSession;
import org.forwhat.bean.WhatComment;
import org.forwhat.bean.WhatCommentDTO;
import org.forwhat.dao.WhatCommentDao;
import org.forwhat.dao.WhatUserDao;
import org.forwhat.framework.annotation.AOP;
import org.forwhat.framework.annotation.InsertBean;
import org.forwhat.framework.annotation.Service;
import org.forwhat.service.WhatCommentService;
import org.forwhat.util.SqlSessionUtilities;

import java.util.List;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.service.Impl
 * @className: WhatCommentServiceImpl
 * @author: LGJ
 * @description: TODO
 * @date: 2023/12/6 14:48
 * @version: 1.0
 */
@Service("WhatCommentServiceImpl")
public class WhatCommentServiceImpl implements WhatCommentService {

    public WhatCommentDao whatCommentDao;


    public WhatUserDao whatUserDao;

    public static void main(String[] args) {
        SqlSession sqlSession = SqlSessionUtilities.openSqlSession();
        WhatCommentDao mapper = sqlSession.getMapper(WhatCommentDao.class);
    }
    @Override
    @AOP("CheckStateAdvice")
    public List<WhatCommentDTO> getCommentByParent(String parent) {
        List<WhatCommentDTO> whatCommentDTOS = whatCommentDao.selectCommentByParent(parent);
        for (WhatCommentDTO whatCommentDTO : whatCommentDTOS) {
            String commentUserId = whatCommentDTO.getCommentUserId();
            String userNameById = whatUserDao.getUserNameById(commentUserId);
            String commentParent = whatCommentDTO.getCommentParent();
            String parentUserName = whatUserDao.getUserNameById(whatCommentDao.selectUserIdByCommentId(commentParent));
            whatCommentDTO.setCommentUserName(userNameById);
            whatCommentDTO.setParentName(parentUserName);
        }
        return whatCommentDTOS;
    }

    @Override
    @AOP("CheckStateAdvice")
    public List<WhatCommentDTO> getCommentByNode(String node) {
        List<WhatCommentDTO> whatComments = getCommentByParent(node);
        for (WhatCommentDTO whatComment : whatComments) {
            String commentId = whatComment.getCommentId();
            List<WhatCommentDTO> commentByNode = getCommentByNode(commentId);
            whatComment.setWhatCommentDTOList(commentByNode);
        }
        return whatComments;
    }

    @Override
    public int sendComment(WhatComment whatComment) {
        int i1 = 0;
        try {
            String latestComment = whatCommentDao.getLatestComment();
            i1 = Integer.parseInt(latestComment.substring(7))+1;
        } catch (Exception e) {
            i1 = 1;
        }
        whatComment.setCommentId("comment"+i1);
        int i = whatCommentDao.insertComment(whatComment);
        return i;
    }

    public WhatCommentDao getWhatCommentDao() {
        return whatCommentDao;
    }

    public void setWhatCommentDao(WhatCommentDao whatCommentDao) {
        this.whatCommentDao = whatCommentDao;
    }

    public WhatUserDao getWhatUserDao() {
        return whatUserDao;
    }

    public void setWhatUserDao(WhatUserDao whatUserDao) {
        this.whatUserDao = whatUserDao;
    }
}
